﻿@using Apps.Web.Core;
@using Apps.Web;
@using Apps.Common;
@using Apps.Models.Sys;
@using Apps.Locale;
@using Wantn.FIS.Enum;
@{
    ViewBag.Title = "PUB_SUPPLIERMETERIEL";
    Layout = "~/Views/Shared/_Index_Layout.cshtml";
    List<permModel> perm = null;
}
<table style="width:100%">
<tbody>
    <tr>
        <td style="width:330px;vertical-align: top">
             <div class="mvctool">
             </div>
            <table id="ListParent"></table>
        </td>        <td style="width:3px;"></td>
        <td style="vertical-align:top">
             <div class="mvctool">
                 @Html.ToolButton("btnAddMater", "fa fa-plus", "添加物料", "Create", true)
                 @Html.ToolButton("btnDelete", "fa fa-trash","移除物料", ref perm, "Create", true)
                 @Html.ToolButton("btnSaveMater", "fa fa-pencil", "保存",ref perm, "Create", true)
             </div>
            <table id="List"></table>
        </td>
    </tr>
</tbody>
</table>

<div id="modalwindow" class="easyui-window" style="width:800px; height:400px;" data-options="modal:true,closed:true,minimizable:false,shadow:false"></div>
<script type="text/javascript">
    $(function () {
        $('#ListParent').datagrid({
            url: '@Url.Action("GetListParent")',
            width:430,
            methord: 'post',
            height: SetGridHeightSub(45),
            fitColumns: true,
            sortName: 'SUPPLIER_NAME',
            sortOrder: 'desc',
            idField: 'SUPPLIER_ID',
            pageSize: 30,
            pageList: [15, 20, 30, 40, 50],
            pagination: true,
            striped: true, //奇偶行是否区分
            singleSelect: true,//单选模式
            rownumbers: true,//行号
            onLoadSuccess: function(data) {
                setTimeout(function () {
                    $('#ListParent').datagrid("selectRow", 0);
                    var row = $('#ListParent').datagrid('getSelected');
                    if (row != null) {
                        $('#List').datagrid("load", { ParentId: row.SUPPLIER_ID });
                    }
                },800);
            },
            columns: [[
                { field: 'SUPPLIER_ID', title: '供应商编号', width: 80,hidden:true },
                { field: 'SUPPLIER_NAME', title: '供应商名称', width: 80, sortable: true },
                //{ field: 'KKSCODE', title: 'KKS编码', width: 80, sortable: true ,hidden:true},
                { field: 'ENABLED', title: '启用状态', width: 80, sortable: true, formatter: function (value) { return EnableFormatter(value) } },
                { field: 'REMARKS', title: '备注', width: 80, sortable: true,hidden:true }
            ]]
         ,onClickRow: function(index, row) {
             $('#List').datagrid("load", { ParentId: row.SUPPLIER_ID });
}
        }).datagrid('getPager');
         $(window).resize(function() {
             resizeLayout();
         });
        $('#List').datagrid({
            url: '@Url.Action("GetList")?parentId=0',
            width:SetGridWidthSub(450),
            methord: 'post',
            height: SetGridHeightSub(45),
            fitColumns: true,
            sortName: 'MATERIEL_NAME',
            sortOrder: 'desc',
            idField: 'MATERIEL_ID',
            pageSize: 15,
            pageList: [15, 20, 30, 40, 50],
            pagination: true,
            striped: true, //奇偶行是否区分
            singleSelect: false,//单选模式
            rownumbers: true,//行号
            onLoadSuccess: function(data) {                @foreach(var r in perm){ if (r.Category == 2) { @Html.Raw("$(this).datagrid('hideColumn','" + r.KeyCode + "'); "); } }
            },
            columns: [[
                { field: 'MATERIEL_ID', title: '物料编号', width: 80,checkbox:true},
                { field: 'MATERIEL_NAME', title: '物料名称', width: 80,sortable:true },
                { field: 'MATERIEL_TYPE_ID', title: '物料类型', width: 80, sortable: true, hidden: true },
                { field: 'MATERIEL_TYPEName', title: '物料类型', width: 80, sortable: true,hidden:true },
                { field: 'STATISTICS', title: '统计类型', width: 80, sortable: true, formatter:@Html.Raw(EnumMvcHelper.GridFormatterFunc<MetStatisticsType>())},
                //{ field: 'KKSCODE', title: 'KKS编码', width: 80,sortable:true },
                { field: 'REMARKS', title: '备注', width: 80,sortable:true },
                { field: 'TemplateName', title: '流程模板', width: 80,sortable:true }
            ]]
        });
    });
    //ifram 返回
    function frameReturnByClose() {
        $("#modalwindow").window('close');
    }
    $("#btnDelete").click(function () {
        var row = $('#List').datagrid('getSelected');
        if (row != null) {
            var deletedData = $('#List').datagrid('getChecked');
            for (var i = deletedData.length - 1; i >= 0; i--) {
                var rowIndex = $('#List').datagrid('getRowIndex', deletedData[i]);
                $('#List').datagrid('deleteRow', rowIndex);
            }
        } else { $.messageBox5s(Lang.Tip, '请选择要移除的物料'); }
    });
    function SetSelectValue(data) {
        var lastAmountIndex = $('#List').datagrid('getRows').length;
        $.each(data, function (i, row) {
            $('#List').datagrid('insertRow', {
                index: lastAmountIndex + 1,
                row: {MATERIEL_ID:row.MATERIEL_ID,MATERIEL_NAME:row.MATERIEL_NAME,MATERIEL_TYPE_ID:row.MATERIEL_TYPE_ID,MATERIEL_TYPEName: row.MATERIEL_TYPEName,STATISTICS: row.STATISTICS,KKSCODE: row.KKSCODE,REMARKS:row.REMARKS,TemplateName:row.TemplateName
                }
            })
        });

    }
    function frameReturnByReload(flag) {
        if (flag)
        {
            $("#List").datagrid('load');
        }
        else
        {
            $("#List,#ListParent").datagrid('reload');
        }
    }
    function frameReturnByMes(mes) {
        $.messageBox5s(Lang.Tip, mes);
    }
    $(function () {
        $("#btnAddMater").click(function () {
            var row = $('#ListParent').datagrid('getSelected');
            if (row != null) {
                $("#modalwindow").html("<iframe width='100%' height='100%' scrolling='auto' frameborder='0'' src='@Url.Action("MaterielSel", "MATERIEL")?SuppID="+row.SUPPLIER_ID+"'></iframe>");
                $("#modalwindow").window({ title: Lang.Create, width: 700, height: 400, iconCls: 'fa fa-plus' }).window('open');
            } else { $.messageBox5s(Lang.Tip, '请选择需要关联的供应商'); }
        });
        $("#btnSaveMater").click(function () {
            var row = $('#ListParent').datagrid('getSelected');
            if (row != null) {
                var inserted =$('#List').datagrid('getChanges', "inserted"); //获取添加状态的行
                var deleted = $('#List').datagrid('getChanges', "deleted");//获取删除状态的行
                var updated = $('#List').datagrid('getChanges', "updated");//获取修改状态的行

                var effectRow = new Object();
                if (inserted.length){
                    effectRow["inserted"] = JSON.stringify(inserted);
                }

                if (deleted.length){
                    effectRow["deleted"] = JSON.stringify(deleted);
                }

                if (updated.length){
                    effectRow["updated"] = JSON.stringify(updated);
                }

                $.ajax({
                url: "@Url.Action("SaveMater")?SupplierID="+row.SUPPLIER_ID,
                type: "Post",
                data: effectRow,
                dataType: "json",
                success: function (data) {
                    if (data.type == 1) {
                        $('#List').datagrid("load", { ParentId: row.SUPPLIER_ID });
                        $.messageBox5s(Lang.Tip, '保存物料对应供应商关系成功！');
                    }
                    else {
                        $.messageBox5s(Lang.Tip, '保存失败，原因:'+data.message);
                    }
                }
            });
            } 
        });

    });

    function resizeLayout()
     {
         setTimeout(function () {
             $('#ListParent').datagrid('resize', {
             }).datagrid('resize', {
                 height: SetGridHeightSub(45)
             });
             $('#List').datagrid('resize', {
             }).datagrid('resize', {
                 width: SetGridWidthSub(450),
                 height: SetGridHeightSub(45)
             });
         },100);
	    }
</script>

